package com.school.generator.base;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.school.generator.enums.ExceptionAbstractEnum;
import com.school.generator.enums.ExceptionEnum;

import java.beans.ConstructorProperties;
import java.util.Objects;

public class GenericResponse<T> {
    public static final String DEFAULT_SUCCESS_CODE = "0";
    public static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
    public static final String DEFAULT_FAILED_CODE = "9999";
    public static final String DEFAULT_FAILED_MESSAGE = "FAILED";
    private String code;
    private String message;
    private T data;

    public static GenericResponse success() {
        return success((Object)null);
    }

    public static <T> GenericResponse success(T data) {
        return success("0", "SUCCESS", data);
    }

    public static <T> GenericResponse success(String code, String message, T data) {
        Objects.requireNonNull(code);
        Objects.requireNonNull(message);
        return builder().code(code).message(message).data(data).build();
    }

    public static GenericResponse failed() {
        return failed("9999", "FAILED");
    }

    public static GenericResponse failed(String message) {
        return failed("9999", message);
    }

    public static GenericResponse failed(String code, String message) {
        return builder().code(code).message(message).build();
    }

    public Boolean successed() {
        return "0".equals(this.getCode());
    }

    @Override
    public String toString() {
        try {
            return (new ObjectMapper()).writeValueAsString(this);
        } catch (JsonProcessingException var2) {
            System.out.println(var2);
        }
        return null;
    }

    public static GenericResponse failed(ExceptionEnum exceptionEnum) {

        return builder().code(exceptionEnum.getCode()).message(exceptionEnum.getMessage()).build();
    }

    public static GenericResponse failed(ExceptionAbstractEnum exceptionEnum) {
        return builder().code(exceptionEnum.getCode()).message(exceptionEnum.getMessage()).build();
    }

    private static String $default$code() {
        return "0";
    }

    private static String $default$message() {
        return "SUCCESS";
    }

    public static <T> GenericResponse.GenericResponseBuilder<T> builder() {
        return new GenericResponse.GenericResponseBuilder();
    }

    public String getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public T getData() {
        return this.data;
    }

    public GenericResponse<T> setCode(String code) {
        this.code = code;
        return this;
    }

    public GenericResponse<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public GenericResponse<T> setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof GenericResponse)) {
            return false;
        } else {
            GenericResponse<?> other = (GenericResponse)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$code = this.getCode();
                    Object other$code = other.getCode();
                    if (this$code == null) {
                        if (other$code == null) {
                            break label47;
                        }
                    } else if (this$code.equals(other$code)) {
                        break label47;
                    }

                    return false;
                }

                Object this$message = this.getMessage();
                Object other$message = other.getMessage();
                if (this$message == null) {
                    if (other$message != null) {
                        return false;
                    }
                } else if (!this$message.equals(other$message)) {
                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof GenericResponse;
    }

    @Override
    public int hashCode() {
        int result = 1;
        Object $code = this.getCode();
        result = result * 59 + ($code == null ? 43 : $code.hashCode());
        Object $message = this.getMessage();
        result = result * 59 + ($message == null ? 43 : $message.hashCode());
        Object $data = this.getData();
        result = result * 59 + ($data == null ? 43 : $data.hashCode());
        return result;
    }

    public GenericResponse() {
    }

    @ConstructorProperties({"code", "message", "data"})
    public GenericResponse(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static class GenericResponseBuilder<T> {
        private boolean code$set;
        private String code;
        private boolean message$set;
        private String message;
        private T data;

        GenericResponseBuilder() {
        }

        public GenericResponse.GenericResponseBuilder<T> code(String code) {
            this.code = code;
            this.code$set = true;
            return this;
        }

        public GenericResponse.GenericResponseBuilder<T> message(String message) {
            this.message = message;
            this.message$set = true;
            return this;
        }

        public GenericResponse.GenericResponseBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public GenericResponse<T> build() {
            return new GenericResponse(this.code$set ? this.code : GenericResponse.$default$code(), this.message$set ? this.message : GenericResponse.$default$message(), this.data);
        }

        @Override
        public String toString() {
            return "GenericResponse.GenericResponseBuilder(code=" + this.code + ", message=" + this.message + ", data=" + this.data + ")";
        }
    }
}
